package com.jumper.mydemo.statics;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.ResolvableType;

import com.jumper.mydemo.my.MyBean;

public class BeanFactoryDemo {
    public static void run() {
        @SuppressWarnings("resource")
        BeanFactory beanFactory = new AnnotationConfigApplicationContext(MyBean.class).getBeanFactory();

		Object bean = beanFactory.getBean("myBean");
		System.out.println("通过名称获取Bean:" + bean);

		ObjectProvider<MyBean> objectProvider = beanFactory.getBeanProvider(MyBean.class);
		System.out.println("获取Bean的Provider:" + objectProvider);

		Class<?> beanType = beanFactory.getType("myBean");
		System.out.println("获取Bean的类型:" + beanType);

		boolean containsBean = beanFactory.containsBean("myBean");
		System.out.println("判断是否包含Bean:" + containsBean);

		boolean isSingleton = beanFactory.isSingleton("myBean");
		System.out.println("判断是否为单例：" + isSingleton);

		boolean isPrototype = beanFactory.isPrototype("myBean");
		System.out.println("判断是否为原型:" + isPrototype);

		boolean isTypeMatch = beanFactory.isTypeMatch("myBean", ResolvableType.forClass(MyBean.class));
		System.out.println("判断是否匹配制定类型:" + isTypeMatch);

		String[] aliases = beanFactory.getAliases("myBean");
		System.out.println("获取Bean的所有别名:" + String.join(",", aliases));
    }
}
